/**
 * 
 */
package es.cloudcast.persistence.jdo.action.basic;

import java.io.Serializable;

import javax.jdo.PersistenceManager;

import es.cloudcast.persistence.PersistenceException;
import es.cloudcast.persistence.dao.jdo.PersistenceManagerFactory;
import es.cloudcast.persistence.jdo.action.Action;

/**
 * Retrieves the object associated to given id
 * 
 * @author Samuel García Martínez <in0de>
 *
 */
public class GetObjectAction implements Action {

	protected Serializable id;
	protected Class<?> clazz; 
	
	public GetObjectAction(Serializable id, Class<?> clazz) {
		this.id = id;
		this.clazz = clazz;
	}
	
	/* (non-Javadoc)
	 * @see es.cloudcast.persistence.jdo.action.AbstractAction#process(es.cloudcast.persistence.jdo.PersistenceManagerFactory)
	 */
	public Object process(PersistenceManagerFactory PMF)
			throws PersistenceException {
		Object instance = null;
		
		try {
			PersistenceManager pManager = 
				PMF.getCurrentPersistenceManager();
			
			instance = pManager.getObjectById(clazz, id);
		} catch(Exception ex) {
			throw new PersistenceException(ex);
		}
		
		return instance;
	}

}
